/*
 *  $Id: KnowledgeOfFire.cs 1220 2010-11-30 16:25:44Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using AgateLib.DisplayLib;

namespace Ragima.Cards001.Fire.Abilities {
    public class KnowledgeOfFire: AbilityCard<HandCardData> {
        public KnowledgeOfFire(): base("001_f_a_knowledge_of_fire") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Fire;
            Target = new HandCardTarget(GetTargetPhrase(), CardIsFireSpell);
            AddCondition(new MeditationCondition(1, false));
        }

        private static bool CardIsFireSpell(TargetRequest request, HandCard target) {
            return target.Card.IsSpell && target.Card.Element == ElementType.Fire;
        }

        private static int GetSpellAddCost(Card card) {
            int cost = card.GetSumMana() / 2;
            if (cost == 0) cost = 1;
            return cost;
        }

        protected override bool IsPossibleAction(TargetArgs<HandCardData> args) {
            int cost = GetSpellAddCost(args.Target.Card.Card);
            bool success = (args.Player.Meditation >= cost);
            if (!success) args.Player.Game.ErrorNotice(MeditationCondition.NotEnough.GetText());
            return success;
        }

        protected override void DoAction(TargetArgs<HandCardData> args) {
            SpellCard spell = args.Target.Card.Card as SpellCard;
            if (spell == null) return;
            int cost = GetSpellAddCost(spell);
            args.Player.DecMeditation(cost);
            args.Player.Hands.Remove(args.Target);
            args.Caster.AddAffect(new KnowledgeOfFireAffect(spell, args.Player, cost));
        }

        private class KnowledgeOfFireAffect: SpellAffect {
            private KnowledgeOfFireCastAbility _ability;
            private Player _caster;

            public KnowledgeOfFireAffect(SpellCard spell, Player caster, int cost): base(true) {
                DefaultDescription = true;
                _ability = new KnowledgeOfFireCastAbility(spell, cost);
                _caster = caster;
                Add(new Affects.AbilityAffect(_ability));
            }

            public override string Id {
                get { return AffectManager.GetCardId<KnowledgeOfFire>(null); }
            }

            public override Card BaseCard {
                get { return _ability; }
            }

            public override bool Dispellable {
                get { return false; }
            }

            public override string DescriptionText {
                get { return Description.GetText(_ability); }
            }

            public override void OnRemove() {
                if (_caster != null)
                    _caster.SecondDeck.Add(_ability.Spell);
            }
        }

        private class KnowledgeOfFireCastAbility: SpellAbilityCard {
            private static readonly Languages.CardPhrase<KnowledgeOfFire> Condition = new Languages.CardPhrase<KnowledgeOfFire>("cast.condition");

            public KnowledgeOfFireCastAbility(SpellCard spell, int cost) : base(spell) {
                AddManaCost(ElementType.Fire, cost);
                AddCondition(new DelegateCondition(Condition.Phrase, Creatures.UniqueThenn.IsCasterForm));
            }
        }
    }
}
